package com.common.security.startegy.impl;

import com.common.model.BusinessUserModel;
import com.common.mysql.Where;
import com.common.security.authentication.Oauth2BusinessPasswordModeAuthentication;
import com.common.security.pojo.Oauth2BusinessPasswordMode;
import com.common.service.BusinessUserService;
import com.common.strategy.AccountTypeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component("businessAccount")
public class BusinessPasswordAccountTypeStrategy implements AccountTypeStrategy<Oauth2BusinessPasswordModeAuthentication, Oauth2BusinessPasswordMode> {
    @Autowired
    private BusinessUserService businessUserService;

    public BusinessPasswordAccountTypeStrategy() {
    }

    public Oauth2BusinessPasswordModeAuthentication login(Oauth2BusinessPasswordMode mode) {
        BusinessUserModel businessUserModel = businessUserService.searchOne(Where.build().field("account").equal(mode.getAccount()).and().field("password").equal(mode.getPassword()).and().field("del_flag").equal("0"));
        return ObjectUtils.isEmpty(businessUserModel) ? null : new Oauth2BusinessPasswordModeAuthentication(businessUserModel.getId());
    }
}

